<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css"  media="all">

    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>



<table style="width: 90%;text-align: center;margin-top: 100px" >
    <tr>
        <td style="width: 30%;text-align: center">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>产品更新</legend>
            </fieldset>

            <form class="layui-form"  id="product"/>
                <input type="hidden" id="pid" name="pid">
                <div class="layui-form-item">
                    <label class="layui-form-label">产品名称</label>
                    <div class="layui-input-block">
                        <input id="pname" type="text" name="pname" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" style="width: 200px">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">产品单位</label>
                    <div class="layui-input-block">
                        <input id="unit" type="text" name="unit" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input" style="width: 200px">
                    </div>
                </div>
                <div class="layui-form-item" style="width: 400px">
                    <label class="layui-form-label">产品类型</label>
                    <div class="layui-input-block">
                        <select name="protype.ptid" lay-filter="aihao" id="proty">

                        </select>
                    </div>
                </div>

                <div class="layui-form-item" style="width: 400px">
                    <label class="layui-form-label">供应商</label>
                    <div class="layui-input-block">
                        <select name="supplier.sid" lay-filter="aihao" id="suppli" >
                        </select>
                    </div>
                </div>



                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button id="submit" type="button" class="layui-btn" lay-submit="" lay-filter="demo1" >立即提交</button>

                    </div>
                </div>

            </form>
        </td>
    </tr>
</table>
<script src="layui/layui.js" charset="utf-8"></script>

<script>

    layui.use('table', function(){
        var form = layui.form;
        var $=layui.jquery;
        var table = layui.table;
        var url=window.location.href;
        var index=url.length;
        var pid=url.substring(index-1,index);

        //监听工具条
        $("#submit").click(function () {
            $.ajax({
                type:'post',
                url:'product/up',
                data:$("#product").serialize(),
                dataType:'json',
                traditional:true,
                success:function (data) {
                    alert("更新成功")
                    window.location.href="product.html"
                }

            })
        })
        $.ajax( {
            url : "/protype/show",
            //data : params,
            dataType : "json",
            cache : false,
            async: false,
            error : function(textStatus, errorThrown) {
                hiAlert("系统ajax交互错误: " + textStatus);
            },
            success :
            //回调函数 根据返回值 赋予下拉框值
                function(eve) {

                    var htm = '<option value="">请选择...</option>';
                    if(eve != null){
                        for(var i=0;i<eve.length;i++){
                            htm +='<option  value="'+eve[i].ptid+'">'+eve[i].ptname+'</option>'

                        }
                    }
                    $("#proty").html(htm);


                }

        });
        $.ajax( {
            url : "/supplier/show",
            //data : params,
            dataType : "json",
            cache : false,
            async: false,
            error : function(textStatus, errorThrown) {
                hiAlert("系统ajax交互错误: " + textStatus);
            },
            success :
            //回调函数 根据返回值 赋予下拉框值
                function(eve) {

                    var htm = '<option value="">请选择...</option>';
                    if(eve != null){
                        for(var i=0;i<eve.length;i++){
                            htm +='<option  value="'+eve[i].sid+'">'+eve[i].sname+'</option>'

                        }
                    }
                    $("#suppli").html(htm);

                    form.render("select");
                }

        });
        $.ajax( {
            url : "/product/show",

            data:pid,
            dataType : "json",
            cache : false,
            async: false,
            error : function(textStatus, errorThrown) {
                hiAlert("系统ajax交互错误: " + textStatus);
            },
            success :
            //回调函数 根据返回值 赋予下拉框值
                function(eve) {
                    $("#pid").val(eve[0].pid);
                    $("#pname").val(eve[0].pname);
                    $("#unit").val(eve[0].unit);
                    $("#proty").find("option[value='"+eve[0].protype.ptid+"']").attr("selected",true);
                    $("#suppli").find("option[value='"+eve[0].supplier.sid+"']").attr("selected",true);
                    form.render("select");
                }

        });

    });



</script>

</html>